var mulu = new Vue({
	el: '#novelcatalogue',
	data: {
		title: 'N/A',
		author: 'N/A',
		updateTime: 'N/A',
		updateChapter: 'N/A',
		abstract: 'N/A',
		catalogue: [],
		localData: {  //使用pass表示该数据为空，所有书对象里都是数组类型，存放最近的三个对象
			latelyType: [{
				id: '',
				title: '',
				icon: '',
				introduce: '',
				go: ""
			}, {
				id: '',
				title: '',
				icon: '',
				introduce: '',
				go: ""
			},{
				id: '',
				title: '',
				icon: '',
				introduce: '',
				go: ""
			},{
				id: '',
				title: '',
				icon: '',
				introduce: '',
				go: ""
			}], //最近浏览的小说类别
			latelyList: [{ // 最近看过的小说
				title: '', // 书名
				href: '',  // 链接
				latelyCatalogue: [{ // 最近看的章节
					chapter: '', // 章节名
					href: ''  // 链接
				},{
					chapter: '', // 章节名
					href: ''  // 链接
				},{
					chapter: '', // 章节名
					href: ''  // 链接
				}]
			},{
				title: '', // 书名
				href: '',  // 链接
				latelyCatalogue: [{ // 最近看的章节
					chapter: '', // 章节名
					href: ''  // 链接
				},{
					chapter: '', // 章节名
					href: ''  // 链接
				},{
					chapter: '', // 章节名
					href: ''  // 链接
				}]
			},{
				title: '', // 书名
				href: '',  // 链接
				latelyCatalogue: [{ // 最近看的章节
					chapter: '', // 章节名
					href: ''  // 链接
				},{
					chapter: '', // 章节名
					href: ''  // 链接
				},{
					chapter: '', // 章节名
					href: ''  // 链接
				}]
			}],
			latelySearch: ['', '', '', '', ''] //最近查询（允许存放5个结果）
		}
	},
	methods: {
		getcatalog: function () {
			var that = this;
			$.ajax({
				type : 'POST',
				url : '/novel/novel_data/',
				dataType : "json",//数据类型为json
				data: {
				    what: 'catalogue',
                    type: $('span.hidden')[0].innerHTML,
                    site: $('span.hidden')[1].innerHTML
                },
				success : function(data){
					data = data.data;
					that.title = data.title;
					that.author = data.author;
					that.updateChapter = data.updateChapter;
					that.updateTime = data.updateTime;
					that.abstract = data.abstract;
					that.catalogue = data.chapters;
					$('img#load').addClass('hidden');
				},
				error:function(msg){
					$('#promptbtn').click();
					console.log("ajax error msg",msg);
				}
			});
        },
		addLatelyCatalogue: function (index, idx) {
			var list = this.localData.latelyList;
			var i = 0, j = 0;
			var dt;
			for(; i < list.length; i++){
				dt = list[i].latelyCatalogue;
				if(this.title === list[i].title){  // 找到了小说名存在的
					for(; j < dt.length; j++) {
						if(dt[j].chapter === this.catalogue[index][idx]) {
                            break;
                        }else if(!dt[j].chapter){
							dt[j] = this.catalogue[index][idx];
							break;
						}
					}
					if(j === dt.length){ // 到最后都没找到重复或空章节名
						dt.shift();
						dt.push(this.catalogue[index][idx])
					}
					break;
				}else if(!list[i].title){  // 找到了空的
					list[i].title = this.title;
					list[i].href = window.location.href;
					for(; j < dt.length; j++) {
						if(!dt[j].chapter){
							dt[j] = this.catalogue[index][idx];
							break;
						}
					}
					break;
				}
			}
            if(i === list.length){ // 遍历到最后一个都没找到重复的或空的小说名
				list.shift();
				list.push({
					title: this.title,
					href: window.location.href,
					latelyCatalogue: [this.catalogue[index][idx],{
						chapter: '',
						href: ''
					},{
						chapter: '',
						href: ''
					}]
				});
			}
			try{
				localStorage.setItem('lately', JSON.stringify(this.localData));
			}catch (err){
				console.log(err);
			}
        }
	},
	mounted: function () {
		$('img#load').removeClass('hidden');
		try {
			var data = localStorage.getItem('lately');
			if(data){
				this.localData = JSON.parse(data);
			}else{
				localStorage.setItem('lately', JSON.stringify(this.localData));
			}
		} catch (err) {
			console.log(err);
		}
		this.getcatalog();
    }
});